package io.renren.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.renren.enums.ResultCodeEnum;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ApiResult<T> {
    public static final int SUCCESS = 200;
    private int code;
    private String message;
    private T data;

    public ApiResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public ApiResult() {

    }

    // 成功响应的静态方法
    public static <T> ApiResult<T> success(T data) {
        return new ApiResult<>(200, "成功", data);
    }

    // 失败响应的静态方法
    public static <T> ApiResult<T> fail(String message) {
        return new ApiResult<>(400, message, null);
    }

    protected static <T> ApiResult<T> build(T data) {
        ApiResult<T> result = new ApiResult<T>();
        if (data != null) {
            result.setData(data);
        }
        return result;
    }

    public static <T> ApiResult<T> build(T body, ResultCodeEnum resultCodeEnum) {
        ApiResult<T> result = build(body);
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        return result;
    }

    public static <T> ApiResult<T> build(Integer code, String message) {
        ApiResult<T> result = build(null);
        result.setCode(code);
        result.setMessage(message);
        return result;
    }
}
